package com.king.wms.controller;

import com.king.wms.mapper.OperatorMapper;
import com.king.wms.pojo.Buyer;
import com.king.wms.pojo.Operator;
import com.king.wms.service.BuyerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class CenterController {

	@Autowired
	private OperatorMapper operatorMapper;

	@RequestMapping(value = "/")
	public void to_login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.getRequestDispatcher("/login.jsp").forward(request,response);
	}


	@RequestMapping(value = "/login")
	public void login(Operator ope ,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Operator ope_temp = operatorMapper.selectByPrimaryKey(ope.getId());
		System.err.println(ope_temp + "=================");
        if(ope_temp.getPassword().equals(ope.getPassword())&&ope_temp.getB1().equals(ope.getB1())){
        	request.getSession().setAttribute("cuser",ope_temp);
        	if(ope_temp.getB1().equals("1")){
				request.getRequestDispatcher("/index.jsp").forward(request,response);
			}else{
				request.getRequestDispatcher("/index_ope.jsp").forward(request,response);
			}
		}else{
        	request.setAttribute("error","用户名或密码错误");
			request.getRequestDispatcher("/login.jsp").forward(request,response);
		}

	}

	@RequestMapping(value = "/to_updatepassword")
	public void to_updatepassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.getRequestDispatcher("/jsp/loc/update_password.jsp").forward(request,response);
	}

	@RequestMapping(value = "/updata_password")
	public void updata_password(String password,String newpassword,String rerepassword,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Operator cuser = (Operator)(request.getSession().getAttribute("cuser"));
		if(newpassword.equals(rerepassword)&&cuser.getPassword().equals(password)){
			Operator cuser0 = new Operator();
			cuser0.setId(cuser.getId());
			cuser0.setPassword(newpassword);
			operatorMapper.updateByPrimaryKeySelective(cuser0);
			request.setAttribute("error","修改密码成功!!!");
		}else{
			request.setAttribute("error","修改密码失败!!!");
//			request.getRequestDispatcher("/jsp/loc/update_password.jsp").forward(request,response);
		}
		request.getRequestDispatcher("/jsp/loc/update_password.jsp").forward(request,response);
	}
}
